package com.xoa.controller.hr.hrPersonnelScheduling;

import com.xoa.dao.common.AppLogMapper;
import com.xoa.model.common.AppLog;
import com.xoa.model.duties.UserPost;
import com.xoa.model.hr.HrStaffInfo;
import com.xoa.model.hr.hrDispatcher.HrDispatcher;
import com.xoa.model.hr.hrPersonnelScheduling.HrShedulingLog;
import com.xoa.model.position.UserJob;
import com.xoa.model.users.Users;
import com.xoa.service.common.SysCodeService;
import com.xoa.service.hr.hrPersonnelScheduling.HrPersonnelSchedulingService;
import com.xoa.service.sms.SmsService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.dataSource.ContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by Klous on 2018/8/7.
 * wangyuumeng
 */
@Controller
@RequestMapping("/hrPersonnelScheduling")
public class HrPersonnelSchedulingController {
    @Resource
    HrPersonnelSchedulingService hrPersonnelSchedulingService;
    @Resource
    SysCodeService sysCodeService;
    @Resource
    SmsService smsService;
    @Resource
    AppLogMapper appLogMapper;
    @RequestMapping("/peopleSchedulingList")
    public String navigationBar(){
        return "app/hr/hrPersonnelScheduling/peopleSchedulingList";
    }

    @RequestMapping("/peopleSchedulingListQuery")
    public String peopleSchedulingListQuery(){
        return "app/hr/hrPersonnelScheduling/peopleSchedulingListQuery";
    }

    @RequestMapping("/peopleSchedulingStatistics")
    public String peopleSchedulingStatistics(){
        return "app/hr/hrPersonnelScheduling/peopleSchedulingStatistics";
    }

    @RequestMapping("/jumpHrDispatcherDetails")
    public String jumpHrDispatcherDetails(){
        return "app/hr/hrPersonnelScheduling/jumpHrDispatcherDetails";
    }

    @RequestMapping("/lookDetails")
    public String lookDetails(){
        return "app/hr/hrPersonnelScheduling/lookDetails";
    }

    @RequestMapping("/selectHrDispatcherDetails")
    @ResponseBody
    public ToJson<HrDispatcher> selectHrDispatcherDetails(Integer staffId,String userId){
        return hrPersonnelSchedulingService.selectHrDispatcherDetails(staffId,userId);
    }
    /**
     *查询人员列表（人事档案人员列表）
     * WANGYUMENG
     * @param userId 用户Id
     * @param deptId 部门I
     * @param projectName 项目名称
     * @param workJob 岗位
     * @param locationAddress 所在位置
     * @param position 职务
     * @return
     */
    @RequestMapping("/queryPeopleSchedulingList")
    @ResponseBody
    public ToJson<HrStaffInfo> queryPeopleSchedulingList(String userId,String deptId,Integer projectName,String workJob,Integer locationAddress,String position,Integer page,Integer pageSize,Boolean useFlag){
         return hrPersonnelSchedulingService.queryPeopleSchedulingList(userId,deptId,projectName,workJob,locationAddress,position, page,pageSize, useFlag);
    }

    @RequestMapping("/selectHrDispatcherList")
    @ResponseBody
    public ToJson<HrDispatcher> selectHrDispatcherList(String userId, String deptId, Integer projectName, String workJob, Integer locationAddress, String position, Integer page, Integer pageSize, Boolean useFlag){
        return hrPersonnelSchedulingService.selectHrDispatcherList(userId,deptId,projectName,workJob,locationAddress,position, page,pageSize, useFlag);
    }

    @RequestMapping("/loadHrDispatcherPages")
    @ResponseBody
    public ToJson<HrDispatcher> loadHrDispatcherPages(Integer page,Integer pageSize){
        return hrPersonnelSchedulingService.loadHrDispatcherPages(page,pageSize);
    }

    @RequestMapping("/selectHrDispatcherDetail")
    @ResponseBody
    public ToJson<HrDispatcher> selectHrDispatcherDetail(String assessScore){
        return hrPersonnelSchedulingService.selectHrDispatcherDetail(assessScore);
    }

    @RequestMapping("/details")
    public String details(HttpServletRequest request,Integer staffId) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        String visitUrl = request.getRequestURI();
        visitUrl += "?staffId=" + staffId;
        Integer smsId = smsService.selectSmsId(request,visitUrl);
        if(smsId!=null){
            int RemidFlag = smsService.getRemidFlagById(smsId);
            if(RemidFlag!=0){
                smsService.setRead(request,visitUrl);
                AppLog appLog = new AppLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users sessionUser = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                    appLog.setUserId(sessionUser.getUid()+"");
                }
                appLog.setTime(new Date());
                appLog.setModule("29");//模块29：人员调度
                appLog.setType("1"); //type 1 暂时存放1
                int smsid = smsService.selectSmsId(request,visitUrl);
                appLog.setOppId(smsid + ""); //操作记录的id,这里设置为SMS_ID
                appLog.setRemark("备注");
                appLogMapper.insertSelective(appLog);
            }else{
                AppLog appLog=new AppLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users sessionUser = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                    appLog.setUserId(sessionUser.getUid()+"");
                }
                appLog.setTime(new Date());
                appLog.setModule("29");//模块29：人员调度
                appLog.setType("1"); //type 1 暂时存放1
                int smsid=smsService.selectSmsId(request,visitUrl);
                appLog.setOppId(smsid+""); //操作记录的id,这里设置为SMS_ID
                appLog.setRemark("备注");
                appLogMapper.insertSelective(appLog);
            }
        }
        return "app/hr/hrPersonnelScheduling/details";
    }

    @RequestMapping("/hrStaffInfo")
    @ResponseBody
    public ToJson hrStaffInfo(HttpServletRequest request,Integer staffId) {
        ContextHolder.setConsumerType("xoa" + (String) request.getSession().getAttribute("loginDateSouse"));
        String visitUrl = request.getRequestURI();
        visitUrl += "?staffId=" + staffId;
        Integer smsId = smsService.selectSmsId(request,visitUrl);
        if(smsId!=null){
            int RemidFlag = smsService.getRemidFlagById(smsId);
            if(RemidFlag!=0){
                smsService.setRead(request,visitUrl);
                AppLog appLog = new AppLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users sessionUser = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                    appLog.setUserId(sessionUser.getUid()+"");
                }
                appLog.setTime(new Date());
                appLog.setModule("29");//模块29：人员调度
                appLog.setType("1"); //type 1 暂时存放1
                int smsid = smsService.selectSmsId(request,visitUrl);
                appLog.setOppId(smsid + ""); //操作记录的id,这里设置为SMS_ID
                appLog.setRemark("备注");
                appLogMapper.insertSelective(appLog);
            }else{
                AppLog appLog=new AppLog();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users sessionUser = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                if (sessionUser != null && !StringUtils.checkNull(sessionUser.getUserId())) {
                    appLog.setUserId(sessionUser.getUid()+"");
                }
                appLog.setTime(new Date());
                appLog.setModule("29");//模块29：人员调度
                appLog.setType("1"); //type 1 暂时存放1
                int smsid=smsService.selectSmsId(request,visitUrl);
                appLog.setOppId(smsid+""); //操作记录的id,这里设置为SMS_ID
                appLog.setRemark("备注");
                appLogMapper.insertSelective(appLog);
            }
        }
        return hrPersonnelSchedulingService.hrStaffInfo(staffId);
    }

    /**
     * 批量进行人员调度操作
     * @param request
     * @param userId
     * @param locationAddress
     * @param projectName
     * @param jobPosition
     * @param workJob
     * @param staffId
     * @return
     */
    @RequestMapping("/insertHrShedulingLogList")
    @ResponseBody
    public BaseWrapper insertHrShedulingLogList(HttpServletRequest request, String userId,String locationAddress,String projectName,String jobPosition,String workJob,String staffId,String assessScore,String assessment,Integer openShut){
        return hrPersonnelSchedulingService.insertHrShedulingLogList(userId,locationAddress,projectName,jobPosition,workJob,staffId,request,assessScore,assessment,openShut);
    }
    /**
     * 查询调度操作记录
     * @param projectName 项目名称
     * @param address 调度位置
     * @param dispatcher 操作人
     * @param userId 调度人
     * @return
     */
    @RequestMapping("/queryHrShedulingLogList")
    @ResponseBody
    public ToJson<HrShedulingLog>  queryHrShedulingLogList(String projectName,String address,String dispatcher,String userId,Integer page,Integer pageSize,Boolean useFlag){
        return hrPersonnelSchedulingService.queryHrShedulingLogList(projectName, address, dispatcher, userId,page,pageSize,useFlag);
    }


    /**
     * 根据项目统计各个地区的人数
     * @param projectName
     * @return
     */
    @RequestMapping("/querySchedulingStatistics")
    @ResponseBody
    public BaseWrapper querySchedulingStatistics(String projectName){
        return hrPersonnelSchedulingService.queryHrShedulingLogList(projectName);
    }


    /**
     * 根据项目统计各个地区的人数
     * @param
     * @return
     */
    @RequestMapping("/queryPostSchedulingStatistics")
    @ResponseBody
    public BaseWrapper queryPostSchedulingStatistics(){
        return hrPersonnelSchedulingService.queryPostSchedulingStatistics();
    }

    @RequestMapping("/hrStaffInfoStatistical")
    @ResponseBody
    public ToJson hrStaffInfoStatistical(String projectName,String locationAddress){
        return hrPersonnelSchedulingService.hrStaffInfoStatistical(projectName,locationAddress);
    }

    /**
     * 获取职务
     * @return
     */
    @RequestMapping("/getUserPost")
    @ResponseBody
    public ToJson<UserPost> getUserPost(){
        return hrPersonnelSchedulingService.getUserPost();
    }

    /**
     * 获取岗位
     * @return
     */
    @RequestMapping("/getUserJob")
    @ResponseBody
    public ToJson<UserJob> getUserJob(){
        return hrPersonnelSchedulingService.getUserJob();
    }
}
